﻿
//===============================ctrl+enter
function isKeyTrigger(e, keyCode) {
    var argv = isKeyTrigger.arguments;
    var argc = isKeyTrigger.arguments.length;
    var bCtrl = false;
    if (argc > 2) {
        bCtrl = argv[2];
    }
    var bAlt = false;
    if (argc > 3) {
        bAlt = argv[3];
    }

    var nav4 = window.Event ? true : false;

    if (typeof e == 'undefined') {
        e = event;
    }

    if (bCtrl &&
        !((typeof e.ctrlKey != 'undefined') ?
            e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)) {
        return false;
    }
    if (bAlt &&
        !((typeof e.altKey != 'undefined') ?
            e.altKey : e.modifiers & Event.ALT_MASK > 0)) {
        return false;
    }
    var whichCode = 0;
    if (nav4) whichCode = e.which;
    else if (e.type == "keypress" || e.type == "keydown")
        whichCode = e.keyCode;
    else whichCode = e.button;

    return (whichCode == keyCode);
}

function ctrlEnter(e, receiveRUId, sendRUId, msgContent, RUNickName) {
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
    if (ie) {
        if (event.ctrlKey && window.event.keyCode == 13) {
            sendMsg(receiveRUId, sendRUId, msgContent, RUNickName);
            tinyMCE.getInstanceById("msgContent").setContent("");
        }
    } else {
    if (isKeyTrigger(e, 13, true)) {
        sendMsg(receiveRUId, sendRUId, msgContent, RUNickName);
        tinyMCE.getInstanceById("msgContent").setContent("");
        }
    }
}
//=============================
function EnterKey(ent) {
    alert("");
    ent = (ent) ? ent : (window.event) ? window.event : "";
    if (ent.keyCode == 13) {
        var btn = $("#btnSendMsg");
        if (btn != ent.srcElement) {
            btn.focus();
            btn.click();
        }
    }
}

function ctrlEnterInGroup(event, receiveGroupId, sendRUId, msgContent, RUGroupRemarkName) {
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
    if (ie) {
        if (event.ctrlKey && window.event.keyCode == 13) {
            sendGroupMsg(receiveGroupId, msgContent, sendRUId, RUGroupRemarkName);
            tinyMCE.getInstanceById("msgContent").setContent("");
        }
    } else {
        if (isKeyTrigger(e, 13, true)) {
            sendMsg(receiveGroupId, msgContent, sendRUId, RUGroupRemarkName);
            tinyMCE.getInstanceById("msgContent").setContent("");
        }
    }
}

//加载好友列表
function loadFriends(RUId) {
    $("#friends").html("<img alt='好友列表加载中' border='0' src='/images/loadingAnimation.gif' />");
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/LoadFriends',
        //超时设置
        timeout: 40000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"RUId":"' + RUId + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            $("#friends").html("");
            $("#friends").append(a.d);
        },
        //异常处理函数
        error: function(msg) {
            alert("对不起，加载好友列表失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//加载群列表

function loadGroups(RUId) {
    $("#friends").html("<img alt='群列表加载中' border='0' src='/images/loadingAnimation.gif' />");
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/LoadGroups',
        //超时设置
        timeout: 40000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"RUId":"' + RUId + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            $("#friends").html("");
            $("#friends").append(a.d);
        },
        //异常处理函数
        error: function(msg) {
            alert("对不起，加载群列表失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}
//发送消息
function sendMsg(receiveRUId, sendRUId, msgContent,runickname) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/SendMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"receiveRUId":"' + receiveRUId + '","sendRUId":"' + sendRUId + '","msgContent":"' + escape(msgContent) + '","RUNickName":"' + runickname + '"}',
        //成功处理函数
        success: function(msg) {
        var a = eval('(' + msg + ')');
        $("#divMsgContent").append("<li><img src='images/bullet.gif' align='absmiddle'/>" + runickname + "在 " + (new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
 + "说 <br/>" + msgContent + "</li>");
            $("#divMsgContent").scrollTop(100000);
        },
        //异常处理函数
        error: function(msg) {
        alert("对不起，发送消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}



//接收消息
function receiveMsg(receiveRUId, sendRUId, isUpdateChatMessages) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/ReceiveMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"receiveRUId":"' + receiveRUId + '","sendRUId":"' + sendRUId + '","isUpdateChatMessages":"' + isUpdateChatMessages + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            if (a.d.length > 0) {
                window.top.document.title = "您有新的好友消息，请查收！";
                $("#gPlayer").attr("src", "/swf/audio-player.swf?audioUrl=/mp3/msg.mp3&autoPlay=true");
                
                
                $("#divMsgContent").append("<li>" + a.d + "</li>");
                
                setTimeout(function() { receiveMsg(receiveRUId, sendRUId, isUpdateChatMessages); }, 5000);
            }
        },
        //异常处理函数
        error: function(msg) {
        //alert("对不起，接收消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}


//接收消息（只读)
function receiveMsgReadOnly(receiveRUId, sendRUId, isUpdateChatMessages) {
    //alert(receiveRUId);
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/ReceiveMessagesReadOnlyService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"receiveRUId":"' + receiveRUId + '","sendRUId":"' + sendRUId + '","isUpdateChatMessages":"' + isUpdateChatMessages + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            if (a.d.length > 0) {
                window.top.document.title = "您有新短消息，请查收！";
                $("#gPlayer").attr("src", "/swf/audio-player.swf?audioUrl=/mp3/msg.mp3&autoPlay=true");
                avatarShow(a.d);

                $('#img' + a.d).live("click", function() {
                    $('#img' + a.d).css("border", "0px");
                    $('#a' + imgId).css("color", "black");
                });

                $('#a' + a.d).live("click", function() {
                    $('#img' + a.d).css("border", "0px");
                    $('#a' + a.d).css("color", "black");
                });

                setTimeout(function() { receiveMsg(receiveRUId, sendRUId, isUpdateChatMessages); }, 5000);
            }
        },
        //异常处理函数
        error: function(msg) {
        //alert("对不起，接收消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//发送群消息


function sendGroupMsg(receiveGroupId, msgContent, sendRUId, RUGroupRemarkName) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/SendGroupMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"receiveGroupId":"' + receiveGroupId + '","msgContent":"' + escape(msgContent) + '","sendRUId":"' + sendRUId + '","RUGroupRemarkName":"' + RUGroupRemarkName + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            $("#divgroupMsgContent").append("<li><img src='images/bullet.gif' align='absmiddle'/>" + RUGroupRemarkName + "在 " + " " + (new Date()).toLocaleTimeString() + " "
 + "说 <br/>" + msgContent + "</li>");
            $("#divgroupMsgContent").scrollTop(100000);
        },
        //异常处理函数
        error: function(msg) {
            alert("对不起，发送群消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//接收群消息


function receiveGroupMsg(receiveGroupId, RUId) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/ReceiveGroupMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"receiveGroupId":"' + receiveGroupId + '","RUId":"' + RUId + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            if (a.d.length > 0) {
                window.top.document.title = "您有新的短消息，请查收！";
                $("#gPlayer").attr("src", "/swf/audio-player.swf?audioUrl=/mp3/msg.mp3&autoPlay=true");


                $("#divgroupMsgContent").append("<li>" + a.d + "</li>");

                setTimeout(function() { receiveGroupMsg(receiveGroupId, RUId); }, 5000);
            }
            
            
            
        },
        //异常处理函数
        error: function(msg) {
            alert("对不起，接收群消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//发送聊天室消息
function sendChattingroomMsg(BUId, msgContent, BUNickName) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/SendChattingRoomMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"BUId":"' + BUId + '","msgContent":"' + escape(msgContent) + '","BUNickName":"' + BUNickName + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            $("#divchattingroomMsgContent").append("<li><img src='images/bullet.gif' align='absmiddle'/>" + BUNickName + "在 " + " " + (new Date()).toLocaleTimeString()
 + "说 <br/>" + msgContent + "</li>");
            $("#divchattingroomMsgContent").scrollTop(100000);
        },
        //异常处理函数
        error: function(msg) {
            alert("对不起，发送消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
        
        
        
    });
}


//接收聊天室信息
function receiveChattingRoomMsg(RecieveTime) {
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/ReceiveChattingRoomMessagesService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"RecieveTime":"' + RecieveTime + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            if (a.d.length > 0) {
                window.top.document.title = "您有新的聊天室消息，请查收！";
                $("#gPlayer").attr("src", "/swf/audio-player.swf?audioUrl=/mp3/msg.mp3&autoPlay=true");


                $("#divchattingroomMsgContent").append("<li>" + a.d + "</li>");

                setTimeout(function() { receiveChattingRoomMsg(RecieveTime); }, 5000);
            }
        },
        //异常处理函数
        error: function(msg) {
            //alert("对不起，接收群消息失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//添加好友
function addFriend() {
    
}

//删除好友
function delFriends(RUId, FRUId) {

    $('#del' + FRUId).text("删除中...");
    $.ajax({
        //传输方式
        type: 'post',
        //wcf地址，根据实际情况进行修改
        url: '/Wcf/WCFservice.svc/DeleteFriendsService',
        //超时设置
        timeout: 10000,
        //数据传输类型
        contentType: 'text/json',
        data: '{"RUId":"' + RUId + '","FRUId":"' +FRUId + '"}',
        //成功处理函数
        success: function(msg) {
            var a = eval('(' + msg + ')');
            if (a.d.length > 0) {
                $('#li' + FRUId).remove();
            }
        },
        //异常处理函数
        error: function(msg) {
        alert("对不起，删除好友失败，服务器超时，请重新操作或向管理员shuofeng@mail.ustc.edu.cn发送邮件报错，我们将会尽快解决，谢谢！");
        }
    });
}

//按设定的时间间隔执行用户是否在线的检测
function checkOnlineFriends() {
}

//接收到消息时头像闪动
function PortraitShow(imgId) {
    //$('#img' + imgId).toggle();
    $('#img' + imgId).css("border", "2px solid #CCCCCC");
    $('#a' + imgId).css("color", "Red");
    //setTimeout(function() { PortraitShow(imgId); }, 1000);
}


function actionOnbeforeunload() {

    return '是否离开当前页面？';
    //event.returnValue = "是否离开当前页面？";
}

function actionOnunload() {

    if (confirm('')) {
        window.parent.top.location.href = 'RUIndex.aspx?action=logout';
    } else {
        return false;
    }
}